﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace ScoreManage
{
    public partial class TeacherChangeInfo : System.Web.UI.Page
    {
        protected string ID;
        protected string strConn = ConfigurationManager.ConnectionStrings["studbConnectionString"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["ID"] == null)
            {
                Response.Redirect("TeacherGradeManage.aspx?item=1");         // 返回到主页面
            }
            else
            {
                ID = Session["ID"].ToString().Replace("'", "''");
                if (!IsPostBack)
                {
                    BindData();
                }
            }
        }

        protected void BindData()
        {
            SqlConnection conn = new SqlConnection(strConn);
            conn.Open();
            string strSql = string.Format("SELECT * FROM teacher WHERE tno = '{0}'", ID);
            SqlCommand myCommand = new SqlCommand(strSql, conn);
            SqlDataReader dr = myCommand.ExecuteReader();
            if (dr.Read())
            {
                txtID.Text = dr["tno"].ToString();
                txtName.Text = dr["tname"].ToString();
                rblGender.SelectedValue = dr["sex"].ToString();
                txtEmail.Text = dr["email"].ToString();
                ddlDepartment.SelectedValue = dr["department"].ToString();
            }
            else
            {
                Response.Redirect("TeacherGradeManage.aspx?item=1");         // 返回到主页面
            }
        }

        // 采用直接执行SQL语句的方法实现添加功能
        protected void btnOK_Click(object sender, EventArgs e)    // 添加
        {
            if (Page.IsValid)
            {
                string ID = txtID.Text.Trim().Replace("'", "''");
                string Name = txtName.Text.Trim().Replace("'", "''");
                string Gender = rblGender.SelectedValue;
                string Email = txtEmail.Text.Trim().Replace("'", "''");
                string Department = ddlDepartment.SelectedValue;


                SqlConnection conn = new SqlConnection(strConn);
                conn.Open();
                string strSql = string.Format("UPDATE teacher SET tname = '{0}', sex = '{1}', email = '{2}', department = '{3}' WHERE tno = '{4}'", Name, Gender, Email, Department, ID);
                SqlCommand myCommand = new SqlCommand(strSql, conn);
                myCommand.ExecuteNonQuery();                            // 执行命令
                Response.Redirect("TeacherGradeManage.aspx?item=1");         // 返回到主页面
            }
        }


        protected void btnReset_Click(object sender, EventArgs e)
        {
            BindData();
        }
    }
}